package org.example.command;

import org.example.model.FarmMachine;
import org.example.strategy.PathPlanningContext;
import org.example.strategy.PathPlanningStrategy;

public class ChangePathCommand implements FarmMachineCommand {
    private FarmMachine machine;
    private PathPlanningContext pathPlanningContext;
    private PathPlanningStrategy newStrategy;
    private PathPlanningStrategy oldStrategy;
    private boolean executed;

    public ChangePathCommand(FarmMachine machine, PathPlanningContext pathPlanningContext,
                             PathPlanningStrategy newStrategy) {
        this.machine = machine;
        this.pathPlanningContext = pathPlanningContext;
        this.newStrategy = newStrategy;
        this.oldStrategy = pathPlanningContext.getStrategy();
        this.executed = false;
    }

    @Override
    public void execute() {
        System.out.println("执行命令: 更改路径规划策略为 " + newStrategy.getStrategyName());
        pathPlanningContext.setStrategy(newStrategy);
        pathPlanningContext.planPath(machine);
        executed = true;
    }

    @Override
    public void undo() {
        if (executed) {
            System.out.println("撤销命令: 恢复路径规划策略为 " +
                    (oldStrategy != null ? oldStrategy.getStrategyName() : "无策略"));
            pathPlanningContext.setStrategy(oldStrategy);
            if (oldStrategy != null) {
                pathPlanningContext.planPath(machine);
            }
            executed = false;
        }
    }

    @Override
    public String getCommandName() {
        return "更改路径规划策略";
    }
}